package fun.werfamily.wf.base.exception;

import fun.werfamily.wf.base.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Mr.WenMing Zhang
 * @Date: 2022/07/27 17:08
 * @Description:
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = BaseCoreException.class)
    public Result handler(BaseCoreException baseCoreException) {
        log.warn("接口异常msg{}", baseCoreException);
        return Result.error(baseCoreException.getCode(), baseCoreException.getMsg(), null);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = RuntimeException.class)
    public Result handler(RuntimeException runtimeException) {
        log.error("接口调用异常cause{}", runtimeException);
        return Result.error(runtimeException.getMessage(), null);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = Exception.class)
    public Result handler(Exception exception) {
        log.error("接口调用异常cause{}", exception);
        return Result.error(exception.getMessage(), null);
    }
}
